// agent-ui/src/api/ai/aiApi.js

const aiApi = {
    // 与豆酱AI聊天 - 流式输出
    async chatWithDoujiangStream(memoryId, message, onChunk, onComplete, onError) {
        try {
            console.log('开始流式请求:', { memoryId, message });
            
            const response = await fetch('http://localhost:9527/agent/doujiang-chat', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    memoryId: memoryId,
                    message: message
                })
            });

            console.log('响应状态:', response.status);
            console.log('响应头:', Object.fromEntries(response.headers.entries()));

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            if (!response.body) {
                throw new Error('Response body is null');
            }

            const reader = response.body.getReader();
            const decoder = new TextDecoder();

            while (true) {
                const { done, value } = await reader.read();
                
                if (done) {
                    console.log('流式读取完成');
                    onComplete();
                    break;
                }

                const chunk = decoder.decode(value, { stream: true });
                console.log('收到数据块:', chunk);
                onChunk(chunk);
            }
        } catch (error) {
            console.error('流式请求错误:', error);
            onError(error);
        }
    },

    // 与通用AI聊天 - 流式输出
    async chatWithAIStream(memoryId, message, onChunk, onComplete, onError) {
        try {
            const response = await fetch('http://localhost:9527/agent/chat', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    memoryId: memoryId,
                    message: message
                })
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const reader = response.body.getReader();
            const decoder = new TextDecoder();

            while (true) {
                const { done, value } = await reader.read();
                
                if (done) {
                    onComplete();
                    break;
                }

                const chunk = decoder.decode(value, { stream: true });
                onChunk(chunk);
            }
        } catch (error) {
            onError(error);
        }
    },

    // 保留原有的非流式方法作为备用
    chatWithDoujiang(memoryId, message) {
        return fetch('http://localhost:9527/agent/doujiang-chat', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                memoryId: memoryId,
                message: message
            })
        }).then(response => response.text());
    },

    chatWithAI(memoryId, message) {
        return fetch('http://localhost:9527/agent/chat', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                memoryId: memoryId,
                message: message
            })
        }).then(response => response.text());
    }
};

export default aiApi; 